package com.sw.servlet;


import com.sw.servlet.util.HttpProtocolUtil;
import com.sw.servlet.util.StaticResourceUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author sw
 * @project minicat
 * @description 响应信息
 * @date 2020/4/29 22:05
 */
public class Response {
    private OutputStream outputStream;

    public Response(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public Response() {
    }

    /**
     * 更具url 获取静态资源的绝对路径 并读取返回
     * @param path
     */
    public void outPut(String path) throws IOException {
        //获取静态资源文件的绝对路径
        String absoluteResourcePath=StaticResourceUtil.getAbsolutePath(path);
        File file=new File(absoluteResourcePath);
        if( file.exists() && file.isFile()){
            StaticResourceUtil.outputStaticResources(new FileInputStream(file),outputStream);
        }else{
            output(HttpProtocolUtil.getHttpHeader404());
        }
    }

    /**
     * 输出制定字符串
     */
    public void output(String content){
        try {
            outputStream.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
